<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Request;
use Hyperf\View\RenderInterface;
use Illuminate\Support\Facades\Hash;
use SmsDemo;

class ServicerController
{

     public function login(Request $request){
           $servicer = DB::table('servicer')
               ->where('status',0)
               ->where('telephone','=',$request->telephone)->first();
           if(!empty($servicer->id)){
                session_start();
                $_SESSION['servicer_id'] = $servicer->id;
                $_SESSION['check_code'] = rand(100000,999999);
                $data = SmsDemo::sendSms($servicer->telephone,$_SESSION['check_code']);
                $data = (array)$data;
                if(!isset($data['Code'])||$data['Code'] !='OK' ){
                    $res['status'] = false;
                    $res['code'] = 400;
                    $res['msg'] = '你发送短信太过于频繁';
                    $res['data']  = [];
                    return json_encode($res,1);
                }
                $_SESSION['servicer_phone'] =   $servicer->telephone;
                $_SESSION['servicer_name'] =  $servicer->name;
                $_SESSION['check_pass'] = 0;
                $_SESSION['network'] = 'http://';
                $_SESSION['REMOTE_ADDR'] = $_SERVER["REMOTE_ADDR"];
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '验证码发送成功';
                $res['token'] = session_id();
                $res['data']  = [];
                return json_encode($res,1);
           }else{
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '你输入的账号或者密码错误';
                $res['data']  = [];
                return json_encode($res,1);
           }
     }

     /*
      *密码登陆
      * */
     public function password_login(Request $request){
            $servicer = DB::table('servicer')
               ->where('status',0)
               ->where('telephone','=',$request->telephone)->first();
           if(!empty($servicer->id)){
                if(Hash::check($request->password,$servicer->password)) {
                        session_start();
                        $_SESSION['servicer_id'] = $servicer->id;
                        $_SESSION['check_code'] = rand(100000,999999);
                        $_SESSION['servicer_phone'] =   $servicer->telephone;
                        $_SESSION['servicer_name'] =  $servicer->name;
                        $_SESSION['network'] = 'http://';
                        $_SESSION['REMOTE_ADDR'] = $_SERVER["REMOTE_ADDR"];
                        $_SESSION['check_pass'] = 1;
                        $servicer->face_url = $_SESSION['network'].$_SERVER['SERVER_NAME'].'/face_url/'.$servicer->face_url;
                        $res['status'] = true;
                        $res['code'] = 200;
                        $res['msg'] = '';
                        $res['token'] = session_id();
                        $res['data']  = (array)$servicer;
                        return json_encode($res,1);
                }else{
                    $res['status'] = false;
                    $res['code'] = 400;
                    $res['msg'] = '登陆失败，账号或则密码错误';
                    $res['data'] = [];
                    return json_encode($res,1);
                }
            }else{
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '你输入的账号或者密码错误';
                $res['data']  = [];
                return json_encode($res,1);
           }

     }




     public function check(Request $request){
           session_id($request->token);
           session_start();

           if(!isset($_SESSION['check_code'])){

                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '你没有发送验证码';
                $res['data']  = [];
                return json_encode($res,1);

           }
           if($request->code == $_SESSION['check_code']){
               $servicer = DB::table('servicer')->select('id','name','face_url','telephone','worker_code')->where('id','=',$_SESSION['servicer_id'])->first();
               $_SESSION['check_pass'] = 1;
                $servicer->face_url = $_SESSION['network'].$_SERVER['SERVER_NAME'].'/face_url/'.$servicer->face_url;
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $res['data']  = (array)$servicer;
                return json_encode($res,1);
           }else{
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '';
                $res['data']  = [];
                return json_encode($res,1);
           }
     }


     /*
      * 查看业绩
      *
      * date
      *
      * pack_mathod
      *
      * */

     public function performan(Request $request){

           $customer = DB::table('customer')
               ->select('id','customer_name','balance')
               ->where('servicer_id',$_SESSION['servicer_id'])
               ->where('status',0)
               ->get();

           $customer_id = [];
           $customer_name = [];
           $customer_balance = [];
           $customer = json_decode($customer,1);
           foreach ($customer as $key=>$value){
                $customer_id[] = $value['id'];
                $customer_name[$value['id']][] = $value['customer_name'];
                $customer_balance[$value['id']][] = $value['balance'];
           }

           /*
            * 获取一下订单
            * */
           $order = DB::table('order')
                 ->select('id')
                 ->whereIn('customer_id',$customer_id)
                 ->where('code','!=',2)
                 ->get();

           $order_id = [];
           foreach ($order as $key=>$value){
                $order_id[] = $value->id;
           }

           $order_product = DB::table('order_product')
               ->select('product_name','standards','price','category','brand','pack_method','product_num','total_sell','customer_id','create_time')
               ->whereIn('order_id',$order_id)
               ->get();

            $order_product = json_decode($order_product,1);
           foreach ($order_product as $key=>$value){
               $order_product[$key]['customer_name'] = $customer_name[$value['customer_id']];
           }
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $res['data'] = $order_product;

                return json_encode($res,1);
     }

     /*
      * 查看业绩图文
      * */
     public function performan_map(){

                $data = DB::table('order')
                 ->select('total_sell','create_time','already_pay')
                 ->where('servicer_id',$_SESSION['servicer_id'])
                 ->where('code','!=',2)
                 ->get();

               $categories = [];
               $series = [];
               $series[0]['name'] = '最低';
               $series[1]['name'] = '最高';
               $series[0]['color'] = '#479FF8';
               $series[1]['color'] = '#83D755';

               foreach ($data as $key=>$value){
                        $categories[] = $value->create_time;
                        $series[0]['data'][] = $value->already_pay;
                        $series[1]['data'][] = $value->total_sell;
               }

           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data']['categories'] = $categories;
           $res['data']['series'] = $series;
           return json_encode($res,1);
     }





     /*
      * 合同查看
      *
      * */

     public function agreement(Request $request){

                if (isset($request->customer_id)){
                    $res['status'] = true;
                    $res['code'] = 200;
                    $res['msg'] = '';
                    $agreement = DB::table('agreement')
                        ->select('*')
                        ->where('customer_id', $request->customer_id)
                        ->where('servicer_id', $_SESSION['servicer_id'])
                        ->get();
                         $agreement = json_decode($agreement,1);
                         if(!empty($agreement)) {
                            foreach ($agreement as $key=>$value){
                                 $customer = DB::table('customer')->select('customer_name')->where('id',$value['customer_id'])->first();
                                 $order = DB::table('order')->select('code')->where('id',$value['order_id'])->first();
                                 $agreement[$key]['customer_name'] = $customer->customer_name;
                                 $agreement[$key]['code'] = $order->code;
                            }

                            $res['data']['agreement'] = $agreement;
                        }

                    if (!empty($agreement)) {
                        $res['data']['agreement'] = $agreement;
                    } else {
                        $res['data']['agreement'] = [];
                    }
                }else {
                    $res['status'] = true;
                    $res['code'] = 200;
                    $res['msg'] = '';
                    $agreement = DB::table('agreement')->select('*')->where('servicer_id', $_SESSION['servicer_id'])->get();
                    $agreement = json_decode($agreement,1);
                    if(!empty($agreement)) {
                    foreach ($agreement as $key=>$value){
                         $customer = DB::table('customer')->select('customer_name')->where('id',$value['customer_id'])->first();
                         $order = DB::table('order')->select('code')->where('id',$value['order_id'])->first();
                         $agreement[$key]['customer_name'] = $customer->customer_name;
                         $agreement[$key]['code'] = $order->code;
                    }

                        $res['data']['agreement'] = $agreement;
                    }

                    if (!empty($agreement)) {
                        $res['data']['agreement'] = $agreement;
                    } else {
                        $res['data']['agreement'] = [];
                    }

                }

                return json_encode($res,1);
     }


     /*
      *不同状态下合同
      * */
     public function agreement_pass(Request $request){
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $agreement = DB::table('agreement')
                    ->select('id','agreement_method','agreement_code','agreement_position','supplyer_company','get_company','customer_id','print_status','order_id')
                    ->where('status',1)
                    ->where('servicer_id',$_SESSION['servicer_id'])
                    ->get();
                $agreement = json_decode($agreement,1);
                if(!empty($agreement)) {
                    foreach ($agreement as $key=>$value){
                         $customer = DB::table('customer')->select('customer_name')->where('id',$value['customer_id'])->first();
                         $order = DB::table('order')->select('code')->where('id',$value['order_id'])->first();
                         $agreement[$key]['customer_name'] = $customer->customer_name;
                         $agreement[$key]['code'] = $order->code;
                    }

                    $res['data']['agreement'] = $agreement;
                }else{
                    $res['data']['agreement'] = [];
                }
                return json_encode($res,1);
     }

      /*
      *不同状态下合同
      * */
     public function agreement_check(Request $request){
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $agreement = DB::table('agreement')
                    ->select('id','create_date','order_id','total_sell','customer_id','order_product_id','print_status','status','order_id')
                    ->where('status',0)
                    ->where('servicer_id',$_SESSION['servicer_id'])
                    ->get();
                $agreement = json_decode($agreement,1);
                if(!empty($agreement)) {
                    foreach ($agreement as $key=>$value){
                         $customer = DB::table('customer')->select('customer_name')->where('id',$value['customer_id'])->first();
                         $order = DB::table('order')->select('code')->where('id',$value['order_id'])->first();
                         $order = (array)$order;
                         $agreement[$key]['customer_name'] = $customer->customer_name;
                         $agreement[$key]['code'] = $order['code'];
                         $order_product_id = explode(',',$value['order_product_id']);
                         $product = DB::table('order_product')->select('category','brand','standards')->where('id',$order_product_id[0])->first();
                         $agreement[$key]['product_name'] = $product->brand.$product->category.$product->standards;
                    }

                    $res['data']['agreement'] = $agreement;
                }else{
                    $res['data']['agreement'] = [];
                }
                return json_encode($res,1);
     }


     /*
      * 合同详情
      * agreement_id
      * */

     public function  agreement_detail(Request $request){

           $agreement  = DB::table('agreement')
               ->select('id','order_id','agreement_method','agreement_code','agreement_date','agreement_order','agreement_position','product_id','customer_id','servicer_id','pic_id','status','supplyer_company','get_company','print_status')
               ->where('id',$request->agreement_id)
               ->where('servicer_id',$_SESSION['servicer_id'])
               ->first();
            $agreement = (array)$agreement;
            if(empty($agreement)){
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '没有合同';
                $res['data'] = [];
                return json_encode($res,1);
            }
            if(!empty($agreement['pic_id'])) {
                $img_id = explode(',', $agreement['pic_id']);
                $img = DB::table('agreement_pic')->select('name')->whereIn('id', $img_id)->get();

                foreach ($img as $key=>$value) {
                    $agreement['images'][$key] = $_SESSION['network'] . $_SERVER['SERVER_NAME'] .'/pay/'.$value->name;
                }

            }else{
                $agreement['images'] =[];
            }
            //订单查看
           $order = DB::table('order')
               ->where('id',$agreement['order_id'])
               ->select('id','order_product_id','customer_id','order_code','code')
               ->first();
            //查出产品 建议新建一个下单后的商品表
           $order = (array)$order;
           $agreement['code'] = $order['code'];
           $product_id = explode(',', $order['order_product_id']);
           $product = DB::table('order_product')
               ->select('id','category','brand','standards','pack_method','supplyer_company','product_num')
               ->whereIn('id',$product_id)->get();

           $customer = DB::table('customer')
               ->select('id','customer_name','telephone','address')
               ->where('id',$order['customer_id'])->first();
           $customer = (array)$customer;

                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $res['data']['order_code'] = $order['order_code'];
                $res['data']['agreement'] =$agreement;
                $res['data']['customer'] =$customer;
                $res['data']['product'] = $product;
                return json_encode($res,1);
     }


     /*
      * 客户添加
      *
      * */

     public function customer_add(Request $request){
              //\Log::debug($request);
             $data['customer_name'] = $request->clientName;
             $data['address'] = $request->clientAddress;
             $data['type'] = $request->clientType;
             $data['telephone'] = $request->telephone;
             $data['servicer_id'] = $_SESSION['servicer_id'];

             if($data['type'] == 1){
                 $data['type_name'] = '现金客户';
             }else if($data['type'] == 2){
                 $data['type_name'] = '公司配送';
             }



             $contact = json_decode($request->contacts,1);
             if(isset($contact[0])){
             $data['contact'] = $contact[0]['name'];
             $data['position'] = $contact[0]['position'];
             $data['contact_telephone'] = $contact[0]['phone'];
             }
             if(isset($contact[1])){
             $data['contact2'] = $contact[1]['name'];
             $data['position2'] = $contact[1]['position'];
             $data['contact_telephone2'] = $contact[1]['phone'];
             }
             //\Log::debug($request);
             //\Log::debug($request->contacts);
             $bool = DB::table('customer')->insert($data);
             if($bool) {
                 $res['status'] = true;
                 $res['code'] = 200;
                 $res['msg'] = '';
                 $res['data'] = [];
             }else{
                 $res['status'] = false;
                 $res['code'] = 400;
                 $res['msg'] = '';
                 $res['data'] = [];
             }
             return json_encode($res,1);
     }

}


?>